function SimpleComment(param) {

	var that = this;
	
	var RETRIEVE_COMMENTS_URL = "/zzzzzz/user/retrieve_comments";
	var DELETE_COMMENT_URL = "/zzzzzz/user/delete_comment";
	
	this.j_comment_container = null;
	this.j_comment_list_container = null;
	this.j_comment_entry = null;
	this.j_comment_entry_close_button = null;
	this.j_comment_add_form = null;
	this.j_comment_textarea = null;
	this.j_comment_showing_status = null;
	this.j_comment_loading_status = null;
	this.j_view_more_container = null;
	this.j_view_more_button = null;
	this.j_comment_unshowing_status = null;
	this.j_no_more_result = null;
	
	var topic_id = 0;
	var offset = 0;
	var cur_page_no = 0;
	
	var cur_showing_no = 0;
	var total = 0;
	
	var jqxhr_retrieve_comments = null;
	
	var comment_saving = false;
	var comment_deleting = false;
	var comments_retrieving = false;
	
	function __construct(p) {
	try{
		for(pkey in p) {
			if(pkey[0] != "_") {
				that[pkey] = p[pkey];
			}
		}
		
		if(p._topic_id) {
			topic_id = p._topic_id;
		}
		if(p._offset) {
			offset = p._offset;
		}
		
		that.j_comment_textarea.focus(commentTextareaFocus).blur(commentTextareaUnfocus).keydown(commentTextareaKeydown);
		that.j_comment_entry.live('mouseenter', commentEntryMouseenter).live('mouseleave', commentEntryMouseleave);
		that.j_comment_entry_close_button.live('click', commentEntryCloseClick);
		that.j_view_more_button.click(viewMoreClick);
		
	}catch(err){
		alert(err);
	}
	}

	function commentTextareaFocus(e) {
	try{
		jq(this).css({ "height" : "28px" });
		var item = jq(this).closest('li');
		item.find("[data-comment-dom-id=comment-user-pic]").show();
		item.find("[data-comment-dom-id=comment-add-tip]").show();
		item.find("[data-comment-dom-id=comment-textarea-container]").css({ "margin" : "0 0 0 40px" });
	
	}catch(err){
		alert(err);
	}
	}
	
	function commentTextareaUnfocus(e) {
	try{
		var val = jq.trim(jq(this).val());
		if(val.length == 0) {
			jq(this).val("").css({ "height" : "14px" });
			var item = jq(this).closest('li');
			item.find("[data-comment-dom-id=comment-user-pic]").hide();
			item.find("[data-comment-dom-id=comment-add-tip]").hide();
			item.find("[data-comment-dom-id=comment-textarea-container]").css({ "margin" : "0" });
		}
	}catch(err){
		alert(err);
	}
	}
	
	function commentTextareaKeydown(e) {
	try{
		if(e.which == 13) {
			e.preventDefault();
			var val = jq.trim(jq(this).val());
			if(val.length > 0) {
				saveComment();
			}
		}
	}catch(err){
		alert(err);
	}
	}
	
	function saveComment() {
	try{
		var url = that.j_comment_add_form.attr("action");
		var content = that.j_comment_textarea.val();
		
		comment_saving = true;
		
		jq.get(url, { "topic_id": topic_id, "content": content }, saveCommentSuccess)
		.error(function() {
	    	alert("Oops! An error occurred while processing your request.");
	    })
	    .complete(function() {
	   		// complete
			comment_saving = false;
	    });

	}catch(err){
		alert(err);
	}
	}
	
	function saveCommentSuccess(data) {
	try{
		that.j_comment_textarea.val("");
		cur_showing_no += 1;
		total += 1;
		updateShowingStatus();
		jq(data).insertAfter(that.j_comment_showing_status);
	}catch(err){
		alert(err);
	}
	}
	
	function retrieve() {
	try{
		if(comments_retrieving) {
			jqxhr_retrieve_comments.abort();
		}
		comments_retrieving = true;
		that.j_comment_loading_status.show();
		jqxhr_retrieve_comments = jq.post(RETRIEVE_COMMENTS_URL, { "topic_id": topic_id, "cur_showing_no": cur_showing_no, "offset": offset }, retrieveSuccess)
		.error(function() {
	    	alert("Oops! An error occurred while processing your request.");
	    })
	    .complete(function() {
	   		// complete
			that.j_comment_loading_status.hide();
			comments_retrieving = false;
	    });
	}catch(err){
		alert(err);
	}
	}
	
	function retrieveSuccess(data) {
	try{
		total = data.total;
		cur_showing_no += data.items.length;
		updateShowingStatus();
		
		jq.each(data.items, function(index, value) {
			jq(value.html).insertBefore(that.j_view_more_container);
		});
		
		if(cur_showing_no < total) {
			that.j_comment_unshowing_status.text(getUnshowingStatus());
			that.j_view_more_container.show();
		}
		else if(cur_page_no > 0){			
			that.j_no_more_result.show();
		}
		cur_page_no += 1;
	}catch(err){
		alert(err);
	}
	}
		
	function viewMoreClick(e) {
	try{	
		e.preventDefault();
		that.j_view_more_container.hide();
		retrieve();
	}catch(err){
		alert(err);
	}
	}
	
	function commentEntryMouseenter(e) {
	try{
		jq(this).find("[data-comment-dom-id=comment-entry-close]").show();
	}catch(err){
		alert(err);
	}
	}
	
	function commentEntryMouseleave(e) {
	try{
		jq(this).find("[data-comment-dom-id=comment-entry-close]").hide();
	}catch(err){
		alert(err);
	}
	}
	
	function commentEntryCloseClick(e) {
	try{
		e.preventDefault();
		var comment_id = jq(this).closest("[data-comment-id]").data("comment-id");
		deleteComment(comment_id);
	}catch(err){
		alert(err);
	}
	}
	
	function deleteComment(comment_id) {
	try{
		if(confirm("Are you sure to delete this comment.")) {	
			comment_deleting = true;
			jq.get(DELETE_COMMENT_URL, { "comment_id": comment_id }, deleteCommentSuccess)
			.error(function() {
	    		alert("Oops! An error occurred while processing your request.");
	    	})
	    	.complete(function() {
	   			// complete
	    		comment_deleting = false;
	    	});
		}
	}catch(err){
		alert(err);
	}
	}
	
	function deleteCommentSuccess(data) {
	try{
		if(data === "UNAUTHENTICATE") {
			alert("Unauthenticated!");
		}else {
			that.j_comment_list_container.find("[data-comment-id=" + data + "]").remove();
			cur_showing_no -= 1;
			total -= 1;
			updateShowingStatus();
			// alert("Your request has been processed successfully!");
		}
	}catch(err){
		alert(err);
	}
	}
	
	function updateShowingStatus() {
	try{
		that.j_comment_showing_status.text(getShowingStatus());
		if(!that.j_comment_showing_status.is(":visible")) {
			that.j_comment_showing_status.show();
		}
	}catch(err){
		alert(err);
	}
	}
	
	function getShowingStatus() {
	try{
		if(total === 0) {
			return "No comment yet";
		}else {
			if(cur_showing_no === total) {
				if(total === 1) {
					return "Showing 1 comment";
				}else {
					return "Showing " + cur_showing_no + " comments";
				}
			}else {
				return "Showing " + cur_showing_no + " of " + total + " comments";
			}
		}
	}catch(err){
		alert(err);
	}
	}
	
	function getUnshowingStatus() {
	try{
		return total-cur_showing_no;
	}catch(err){
		alert(err);
	}
	}

	__construct(param);
	
	retrieve();
	
}

